#include <stdio.h>
#include <stdlib.h>
#define BOOKMAX 10
#define USRMAX 4
typedef struct databases
{
    int quantity;
    int nowquant;
    int status; //0:no reg 1:reg
    char name[12];
    char auth[12];
    float price;
}BOOK;
typedef struct student
{
    int rank;   //0:su  1:stu
    int status; //0:no reg 1:reg
    int sex;    //1:man 2:woman
    int stuid;
    char name[24];
}STU;

void regusr(STU *L)
{
    int id;
    char rank;
    char sex;
    int stuid;
    reg1:
    printf("Please input new id [1~%d]:",USRMAX-1);
    scanf("%d",&id);
    if (L[id].status==0)
    {
        L[id].status=1;
    }
    else
    {
        printf("This id is been regen..\nPlease input new ones..\n");
        goto reg1;
    }
    printf("Please input your name:");
    scanf("%s",L[id].name);
    getchar();
//    setbuf(stdin,NULL);
reg2:
    printf("Are you a student?(Y/N)");
    scanf("%c",&rank);
    getchar();
    if (rank =='Y')
    {
            L[id].rank=1;
            printf("Please input your stuid:");
            scanf(" %d",&stuid);
            getchar();
            L[id].stuid=stuid;
    } else
        {
            if (rank =='N')
            L[id].rank=0;
            else{
                printf("you select the wrong choise..\n");
                goto reg2;
            }
        }
    reg3:
    printf("Are you a man?(Y/N)");
    scanf("%c",&sex);
    switch (sex)
    {
        case 'Y':
        {
            L[id].sex=1;
            break;
        }
        case 'N':
        {
            L[id].sex=2;
            break;
        }
        default:
        {
            printf("ERROR\n");
            goto reg3;
        }
    }
    printf("END\n");
}   //sign up users
void showusr(STU *S)
{
    for (int i = 0; i <=USRMAX; ++i)
    {
        if (S[i].status==1)
        {
            if (S[i].rank==0)
            {
            printf("%d\tadminister\tname:%s\n",i,S[i].name);
            }
            else
            {
                printf("%d\tstuid:%d\tname:%s\n",i,S[i].stuid,S[i].name);
            }
        }
    }
}   //show users
void regbook(BOOK *L)
{
    int num;
    printf("Please input this book's ID(1~%d)\nThis ID will been a number of books\nBook ID:",BOOKMAX-1);
    rgb1:
    scanf("%d",&num);
    if (L[num].status==0)
    {
        L[num].status=1;
        printf("Please input book title:");
        scanf("%s",L[num].name);
        printf("Who writen it:");
        scanf("%s",L[num].auth);
        printf("How many about it:");
        scanf("%d",&L[num].quantity);
        printf("How much it:");
        scanf("%f",&L[num].price);
        getchar();
        L[num].nowquant=L[num].quantity;
        printf("END\n");
    }
    else
    {
        printf("uh oh...This ID have been regen\nPlease make new one..");
        goto rgb1;
    }
}   //sign up books
void showbook(BOOK *L)
{
    for (int i = 0; i <=BOOKMAX; ++i)
    {
        if (L[i].status==1)
        {
            printf("%d\tBook name:%s\tBook author:%s\tQuantity:%d\tNow quantity:%d\tprice:%5f\n",i,L[i].name,L[i].auth,L[i].quantity,L[i].nowquant,L[i].price);
        }
    }
}   //show information about books
void showavalue(BOOK *L)
{
    for (int i = 0; i <=BOOKMAX; ++i)
    {
        if (L[i].status==1)
        {
            if (L[i].quantity>=0)
            {
                printf("%d\tBook name:%s\tBook author:%s\tQuantity:%d\tprice:%5f\n",i,L[i].name,L[i].auth,L[i].nowquant,L[i].price);
            }
        }
    }
}   //show information about now valuable books
void borw(BOOK *L)
{
    int id;
    borw1:
    printf("Put your book ID whitch you want:");
    scanf("%d",&id);
    getchar();
    if (L[id].status==1&&L[id].nowquant>0)
    {
        L[id].nowquant--;
        printf("SUCCESS!\n");
    }
    else
    {
        char choice;
        printf("oh uh,This book has no quantity..\nWould you want a new one(Y/N):");
        choice=getchar();
        switch (choice)
        {
            case 'Y':
            {
                goto borw1;
            }
            case 'N':
            {
                printf("END\n");
                break;
            }
        }
    }
}
void rebow(BOOK *L)
{
    int id;
    rebow1:
    printf("Please input ID which you have:");
    getchar();
    if (L[id].status==1&&L[id].quantity>=0&&L[id].quantity>=L[id].nowquant)
    {
        L[id].nowquant++;
        printf("SUCCESS!\n");
    }
    else
    {
        char choice;
        printf("un oh,this book have now borwed record\nAre you want another ones(Y/N):");
        scanf("%c",&choice);
        getchar();
        switch (choice)
        {
            case 'Y':
            {
                goto rebow1;
            }
            case 'N':
            {
                printf("END\n");
                break;
            }
        }
    }
}
void rmbook(BOOK *L)
{
    int id;
    char check,choice;
    v1:
    printf("Please input ID:");
    scanf("%d",&id);
    getchar();
    if (L[id].status==1)
    {
        printf("%d\tBook name:%s\tBook author:%s\tQuantity:%d\tNow quantity:%d\tprice:%5f\nConfirm(Y/N)",id,L[id].name,L[id].auth,L[id].quantity,L[id].nowquant,L[id].price);
        check=getchar();
        switch (check)
        {
            case 'Y':
            {
                L[id].status=0;
                printf("END");
                break;
            }
            case 'N':
            {
                printf("EXIT\n");
                break;
            }
        }
    }
    else
    {
        printf("ERROR:No this book..\n");
        goto v1;
    }
}
int main()
{
    int choicen;
    BOOK bookdatas[BOOKMAX];
    STU usrs[USRMAX];
    for (int n = 0; n <= BOOKMAX; ++n)
    {
        bookdatas[n].status=0;
        bookdatas[n].nowquant=0;
    }
    for (int j = 0; j <=USRMAX; ++j)
    {
        usrs[j].status=0;
    }
    v1:
    printf("################################################\n");
    printf("#        WELCOME   BOOK   MANAGER   SYSTEM     #\n");
    printf("#               1.Sign up book                 #\n");
    printf("#               2.Sign up user                 #\n");
    printf("#               3.Sign down book               #\n");
    printf("#               4.borrow a book                #\n");
    printf("#               5.unborrow a book              #\n");
    printf("#               6.show valuable book           #\n");
    printf("#               7.quit                         #\n");
    printf("################################################\n");
    printf("Select number:");
    scanf("%d",&choicen);
    getchar();
    switch (choicen)
    {
        case 1:
        {
            regbook(bookdatas);
//            system("clear");
            goto v1;
        }
        case 2:
        {
            regusr(usrs);
            goto v1;
        }
        case 3:
        {
            rmbook(bookdatas);
            goto v1;
        }
        case 4:
        {
            borw(bookdatas);
            goto v1;
        }
        case 5:
        {
            rebow(bookdatas);
            goto v1;
        }
        case 6:
        {
            showavalue(bookdatas);
            goto v1;
        }
        case 7:
        {
            printf("Exit");
            exit(0);
        }
    }

}
